import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_van_android/utils/router_manager.dart';
import 'package:mmkv/mmkv.dart';
import 'package:oktoast/oktoast.dart';

void main() async {
  await MMKV.initialize();
  EasyRefresh.defaultHeaderBuilder = () => ClassicHeader(
        dragText: '下拉刷新',
        armedText: '释放刷新',
        readyText: '正在刷新...',
        processingText: '正在刷新...',
        processedText: '刷新成功',
        noMoreText: '没有更多',
        failedText: '刷新失败',
        messageText: '上次更新于 %T',
      );

  EasyRefresh.defaultFooterBuilder = () => ClassicFooter(
        dragText: '上拉加载',
        armedText: '释放加载',
        readyText: '正在加载...',
        processingText: '正在加载...',
        processedText: '加载成功',
        noMoreText: '没有更多',
        failedText: '加载失败',
        messageText: '上次更新于 %T',
      );

  runApp(const MyApp());
}

/// 设计尺寸
Size get designSize {
  final firstView = WidgetsBinding.instance.platformDispatcher.views.first;
  // 逻辑短边
  final logicalShortestSide =
      firstView.physicalSize.shortestSide / firstView.devicePixelRatio;
  // 逻辑长边
  final logicalLongestSide =
      firstView.physicalSize.longestSide / firstView.devicePixelRatio;
  // 缩放比例 designSize越小，元素越大
  const scaleFactor = 0.95;
  // 缩放后的逻辑短边和长边
  return Size(
      logicalShortestSide * scaleFactor, logicalLongestSide * scaleFactor);
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return OKToast(
        duration: Duration(seconds: 2),
        position: ToastPosition.bottom,
        backgroundColor: Colors.black87,
        textStyle: TextStyle(fontSize: 16, color: Colors.white),
        textPadding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        //屏幕适配父组件组件
        child: ScreenUtilInit(
          designSize: designSize,
          builder: (_, child) => MaterialApp(
            navigatorKey: navigatorKey,
            title: 'Flutter Van Android',
            routes: RouterManager.routes,
            initialRoute: RouterManager.INDEX,
            navigatorObservers: [RouteLogger()],
            theme: ThemeData(
              colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
              useMaterial3: true,
              bottomNavigationBarTheme: BottomNavigationBarThemeData(
                elevation: 10,
                type: BottomNavigationBarType.fixed,
                enableFeedback: true,
                selectedLabelStyle: TextStyle(fontSize: 14),
                unselectedLabelStyle: TextStyle(fontSize: 14),
              ),
              splashFactory: NoSplash.splashFactory,
            ),
            debugShowMaterialGrid: false,
            debugShowCheckedModeBanner: false,
          ),
        ));
  }
}
